 //*****************************************************************************
//**************** (C) 2008 by Eleven Engineering Incorporated ****************
//*****************************************************************************
//**
//**  Tabs: This file looks best with tab stops set every 6 spaces.
//**
//*****************************************************************************
//*****************************************************************************
//**
//** File:  Thread1_Code.asm
//** Project: Simple Sample Project for WHAM4
//** Revised:
//**
//** Description: Code for Thread1
//**
//**
//**
//**
//*****************************************************************************
//*****************************************************************************


Enter_Thread1:

//-----------------------------------------------
// Setup the stack pointer for thread1
  mov   r7,T1_SP

  //FINAL ---- Output Configurations for the  Remote Control Board BUttons
  mov  r0, 0x0100  //Pin0 as output (LCD control line), pin a1-a4 as input (buttons)
  outp  r0, GPAcfg
  
  mov r0, 0x0000 //Pin c0, c1, 17 as input (buttons)
  outp r0, GPCcfg
  
  mov r0, 0x0000 //PIN D7 as input (buttons)
  outp r0, GPDcfg
  //END Configuration for Buttons

DisplayLogo:
  
  mov r1, 20
StartWait:		//Lets the LCD and other peripherls Power up
  jsr r6, DelayReallyLong
  add r1, r1, -1
  bc ZC, StartWait
  
  
  mov r1, 'N'
  jsr r6, putUART
  jsr r6, DelayReallyLong
 
 //Display ElevenEnggLogo
 mov r1, 'B'
 jsr r6, putUART
 
 mov r1, 255
 jsr r6, putUART
 
 mov r2, 255 //Counter for bitmap display
 mov r3, 0x0000
 mov r0, BitmapLogoV2
 
 BitmapDisplay:
	ld r1,r0,0
	jsr r6, putUART
 	add r0, r0, 1
 	add r2, r2, -1
 	bc ZS, BitmapDisplay2
 	bra BitmapDisplay
 	
 	BitmapDisplay2:
  	mov r1, 'B'
 	jsr r6, putUART
 
 	mov r1, 249
 	jsr r6, putUART
 	mov r2, 249
 	mov r3, 0x0000

	BitmapDisplay3:
	
	ld r1,r0,0
	jsr r6, putUART
 	add r0, r0, 1
 	add r2, r2, -1
 	bc ZS, finishedBitmapDisplay
 	bra BitmapDisplay3
///////////////////done displaying bitmap

finishedBitmapDisplay:
	mov 	r1, 50
	LogoDisplayWait:
	jsr 	r6, DelayReallyLong
 	add 	r1, r1, -1
	bc 	ZC, LogoDisplayWait
	
	mov	r0, SplashScreen
	jsr	r6, DrawMenu
	
	///////// Sound code ///////////
	//my p values to send are 199 132 99 79
	//sound format is T, p, e, t
	mov	r1, 'T'
	jsr 	r6, putUART
	mov	r1, 17	//frequency select value
	jsr	r6, putUART
	mov	r1, 10	//e prescale value
	jsr	r6, putUART
	mov	r1, 22  //tone duration
	jsr	r6, putUART
	jsr 	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	
	mov	r1, 'T'
	jsr 	r6, putUART
	mov	r1, 15	//frequency select value
	jsr	r6, putUART
	mov	r1, 10	//e prescale value
	jsr	r6, putUART
	mov	r1, 22  //tone duration
	jsr	r6, putUART
	jsr 	r6, DelayReallyLong
	jsr 	r6, DelayReallyLong
	jsr 	r6, DelayReallyLong
	jsr 	r6, DelayReallyLong
	
	
	mov	r1, 'T'
	jsr 	r6, putUART
	mov	r1, 13	//frequency select value
	jsr	r6, putUART
	mov	r1, 10		//e prescale value
	jsr	r6, putUART
	mov	r1, 22  //tone duration
	jsr	r6, putUART
	
	//end of sound code
	jsr 	r6, DelayReallyLong
	jsr 	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	
	jsr	r6, GetButtonEvent
	and	r1, r1, r1
	bc	ZC, MainMenuLoop
	
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	
	jsr	r6, GetButtonEvent
	and	r1, r1, r1
	bc	ZC, MainMenuLoop
	
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	
	jsr	r6, GetButtonEvent
	and	r1, r1, r1
	bc	ZC, MainMenuLoop
	
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	jsr	r6, DelayReallyLong
	//Add Music stuff that makes boop dee boop sound or something
	
MainMenuLoop:
	//jsr	r6, ClearScreen
	mov	r1, 'N'
	jsr	r6, putUART
	jsr	r6, DelayReallyLong
	
	mov 	r0, MainMenu
	jsr 	r6, DrawMenu 			//Draws Main Menu
	
//  D7   A1   A2    A3   A4   C0    C1   C7     <-- use this one
// TLeft TMid TRght Rght LFT  Up  Dwn  Centre   //USE THIS CONNECTION
//testing for up/down press, Bit 2, 1	

MMScrollLoop:
	
	mov	r4, 	0x003C //Scroll Mask for Main Menu, rows 3 4 5 6 are scrollable (bits 5 4 3 2 set)
	jsr 	r6, 	CursorMenuLoop //this will return with r2 as the menu pressed, otherwise loops to infinity
	add	r1, r2, -4
	bc	ZS, SetupModulesBranch
	add	r1, r2, -3
	bc	ZS, OverrideBranch
	add	r1, r2, -2
	bc	ZS, ViewStatusBranch 
	add	r1, r2, -99 //Someone Pressed Back, we're at main Menu, IGNORE

bra	MainMenuLoop	//this is in case some other menu gets selected by accident



///////SELECTMODULE MENU CODE///////////////////
SetupModulesBranch: 
	//we're going to be selecting module, then selecting key, then selecting presets, biggest menu structure >_<
	
	mov   r0, SelectModuleMenu
	jsr	r6, DrawMenu
	jsr	r6, DelayReallyLong	//draws the selectmodule menu asking user to select
	
SetupModuleScrollLoop:
	mov 	r4, 0x003F //Scrolling Mask for Program Menu, all but first row.
	jsr 	r6, CursorMenuLoop
	
	add	r1, r2, -2
	bc	ZC, SMNext 
	//Bedroom was selected
	mov	r1, 0x0002
	st	r1, ModuleSelected
	bra KeySelectBranch
SMNext:
	add	r1, r2, -1
	bc	ZC, SMNext1 
	//livingroom was selected
	mov	r1, 0x0001
	st	r1, ModuleSelected
	bra KeySelectBranch

SMNext1:
	add	r1, r2, -99 		//Someone Pressed Back, go to main menu
	bc	ZS, MainMenuLoop 
	
bra	SetupModulesBranch // Add this as a catch all in case something nukes


//////////KeySelectMenu Code////////////
KeySelectBranch:
	mov 	r0, KeySelectMenu
	jsr 	r6, DrawMenu
	jsr 	r6, DelayReallyLong
KeySelectScrollLoop:
	//selectable row 2, or row 5
	mov 	r4, 0x0012  //This mask or subroutine failed. TODO Check
	jsr 	r6, CursorMenuLoop
	jsr 	r6, DelayReallyLong
	
	add	r1, r2, -2
	bc 	ZS, KeySelectMenu1Branch
	
	add	r1, r2, -4
	bc	ZS, KeySelectMenu1Branch  //This second menu isnt actually implemnted, so branches to teh same one
	
	add	r1, r2, -99 		//Someone Pressed Back, go to Select Module
	bc	ZS, SetupModulesBranch 
	
	bra KeySelectScrollLoop
	
///////// KeySelectMenu1 Code (where we select which key we want to change)
// key 1 key 2 key 3 key 4
KeySelectMenu1Branch:
	mov 	r0, KeySelectMenu1
	jsr 	r6, DrawMenu
	jsr 	r6, DelayReallyLong
	
KeySelectMenuScrollLoop:
	mov r4, 0x001E
	jsr r6, CursorMenuLoop
	
	add	r1, r2, -2
	bc	ZC, KSMNext 
	//Key1
	mov	r1, 0x0001
	st	r1, KeySelected
	bra SetPresetsBranch
KSMNext:
	add	r1, r2, -3
	bc	ZC, KSMNext1 
	//ley2
	mov	r1, 0x0002
	st	r1, KeySelected
	bra SetPresetsBranch

KSMNext1:
	add	r1, r2, -4
	bc	ZC, KSMNext2
	//key3
	mov	r1, 0x0003
	st	r1, KeySelected
	bra SetPresetsBranch

KSMNext2:
	add	r1, r2, -5
	bc	ZC, KSMNext3
	//key4
	mov	r1, 0x0004
	st	r1, KeySelected
	bra SetPresetsBranch

KSMNext3:	
	add	r1, r2, -99 		//Someone Pressed Back, go to Select Module
	bc	ZS, SetupModulesBranch 


////////// Set Presets Branch Menu Code///////

SetPresetsBranch:
	
	mov 	r0, SetPresetsMenu
	jsr 	r6, DrawMenu
	jsr 	r6, DelayReallyLong
	
SetPresetScrollLoop:
	mov r4, 0x001C
	jsr r6, CursorMenuLoop
//Itemselected , 3 = temp, 5=light, 6=relay,
	add	r1, r2, -4
	bc	ZC, SPNext 
	//temp selected
	mov	r1, 0x0001
	st	r1, ItemSelected
	bra SetValueBranch
SPNext:
	add	r1, r2, -2
	bc	ZC,SPNext1 
	//Light selected
	mov	r1, 0x0002
	st	r1, ItemSelected
	bra SetValueBranch

SPNext1:
	add	r1, r2, -3
	bc	ZC, SPNext2
	//relay selected
	mov	r1, 0x0003
	st	r1, ItemSelected
	bra SetValueBranch


SPNext2:	
	add	r1, r2, -99 		//Someone Pressed Back, go to Select Key
	bc	ZS, KeySelectMenu1Branch 

//////SetValueBranch where we set values branchCODE //////////////

SetValueBranch:

	mov	r0, SetValueMenu
	jsr 	r6, DrawMenu
	jsr 	r6, DelayReallyLong

SetValueScrollLoop: ///Need to call a "Set Value timer ticker thing that moves numbers up/down, and recognizes if its temp or light (decimal or 0-100)
	bra MainMenuLoop //Just for debug
///////////////

OverrideBranch:
	mov 	r1, 'S'
	jsr 	r6, putUART
	mov	r1, 1
	jsr	r6, putUART
	mov	r1, 'o'
	jsr   r6, putUART
	bra @
	sub	sp, sp, -2   //this is bad, has to be changed


ViewStatusBranch: 	//this is bad, has to be changed
	mov 	r1, 'S'
	jsr 	r6, putUART
	mov	r1, 1
	jsr	r6, putUART
	mov	r1, 'v'
	jsr	r6, putUART
	bra @
	sub	sp, sp, -2
	
	
	
	
	
	
	
	
	
	
 bra @ 
   //////////////END OF CODE FOR THREAD1
   
   
   
   
///////////////////////////////////////
Display_CheckFlag:
  ld r3,CounterFlag
  bc NC,Display_CheckFlag //wait for Thread2 to update counter
       //check if set
  ld r1,Result
  jsr r6,XPD_EchoHex
  mov r1,MSG_NEWLINE
  jsr r6,XPD_EchoString

  bic r3,r3,15   //clear CounterFlag bit
  st r3,CounterFlag

  bra Display_CheckFlag
endbranch:
  bra @    //breakpoint

